﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HotDealD2.Models.ModelView;
namespace HotDealD2.Models.ModelManager
{
    public class DealModel
    {
        private QLDataBaseDataContext dataContext;
        public DealModel()
        {
            dataContext = new QLDataBaseDataContext();
        }

        public IList<Deal> GetAllDeal()
        {
            var listDeal = from list in dataContext.Deals select list;
            return listDeal.ToList();
        }
        public IList<ALLDealView> GetAllDeal(int loaideal)
        {
            var listDeal = new List<Deal>();
            if (loaideal == 0)
            {
                listDeal = (from list in dataContext.Deals select list).ToList();
            }
            else
            {
                listDeal = (from deal in dataContext.Deals
                            where deal.MaLoaiDeal == loaideal
                            select deal
                                ).ToList();
            }
            var listView = new List<ALLDealView>();
            foreach (Deal deal in listDeal)
            {
                ALLDealView temp = new ALLDealView();
                temp.UsernameDoanhNghiep = deal.UsernameDoanhNghiep;
                temp.MaDeal = deal.MaDeal;
                temp.MaLoaiDeal = deal.MaLoaiDeal;
                temp.MaLoaiHinhGiaoDich = deal.MaLoaiHinhGiaoDich;
                temp.GiaTien = String.Format("{0:0,0}", deal.GiaTien);
                temp.GiaGoc = String.Format("{0:0,0}", deal.GiaGoc);
                temp.SoLuongMuaGiaTot = deal.SoLuongMuaGiaTot;
                temp.SoLuongMuaHienTai = deal.SoLuongMuaHienTai;
                temp.SoLuongMuaToiDa = deal.SoLuongToiDa;
                temp.DiemNoiBat = deal.DiemNoiBat;
                temp.ThongTinChiTiet = deal.ThongTinChinh;
                temp.DieuKien = deal.DieuKien;
                temp.TietKiem = Decimal.ToInt32(deal.GiaGoc - deal.GiaTien) * 100 / Decimal.ToInt32(deal.GiaGoc);

                var listImage = (
                                    from image in dataContext.PathImageDeals
                                    where image.MaDeal == deal.MaDeal
                                    select image
                                ).ToList();

                for (int i = 0; i < listImage.Count; i++)
                {
                    temp.PathImages.Add(listImage[i].Path);
                }
                listView.Add(temp);
            }
            return listView;
        }

        public IList<DealChuaDuyet> GetAllDealChuaDuyet()
        {
            var listDeal = from list in dataContext.DealChuaDuyets select list;
            return listDeal.ToList();
        }

        public IList<DealDaDuyet> GetAllDealDaDuyet()
        {
            var listDeal = from list in dataContext.DealDaDuyets select list;
            return listDeal.ToList();
        }

        
        public bool InsertDeal(DealDaDuyet deal)
        {
            try
            {
                Deal newDeal = new Deal();
                
                newDeal.TenDeal = deal.TenDeal;
                newDeal.UsernameDoanhNghiep = deal.UsernameDoanhNghiep;
                newDeal.MaLoaiHinhGiaoDich = deal.MaLoaiHinhGiaoDich;
                newDeal.MaLoaiDeal = deal.MaLoaiDeal;
                newDeal.GiaGoc = deal.GiaGoc;
                newDeal.GiaTien = deal.GiaTien;
                newDeal.NgayDang = deal.NgayDang;
                newDeal.NgayKetThuc = deal.NgayKetThuc;
                newDeal.SoLuongToiDa = deal.SoLuongToiDa;
                newDeal.SoLuongMuaGiaTot = deal.SoLuongMuaGiaTot;
                newDeal.TieuDeSanPham = deal.TieuDeSanPham;
                newDeal.ThongTinChinh = deal.ThongTinChinh;
                newDeal.DieuKien = deal.DieuKien;
                newDeal.DiemNoiBat = deal.DiemNoiBat;
                newDeal.SoLuongMuaHienTai = 0;
                dataContext.Deals.InsertOnSubmit(newDeal);

                var listImage = (from image in dataContext.PathImageDaDuyets
                                 where image.Username == deal.UsernameDoanhNghiep && image.TenDeal == deal.TenDeal
                                 select image
                                ).ToList();
                foreach (PathImageDaDuyet image in listImage)
                {
                    PathImageDeal temp = new PathImageDeal();
                    temp.MaDeal = newDeal.MaDeal;
                    temp.Path = image.Path;

                    dataContext.PathImageDeals.InsertOnSubmit(temp);
                }
                dataContext.SubmitChanges();
                return true;
            }
            catch {
                return false;
            }
        }
        public bool InsertDealDaDuyet(DealChuaDuyet deal)
        {
            try
            {
                DealDaDuyet newDeal = new DealDaDuyet();

                newDeal.TenDeal = deal.TenDeal;
                newDeal.UsernameDoanhNghiep = deal.UsernameDoanhNghiep;
                newDeal.MaLoaiHinhGiaoDich = deal.MaLoaiHinhGiaoDich;
                newDeal.MaLoaiDeal = deal.MaLoaiDeal;
                newDeal.GiaGoc = deal.GiaGoc;
                newDeal.GiaTien = deal.GiaTien;
                newDeal.TieuDeSanPham = deal.TieuDeSanPham;
                newDeal.ThongTinChinh = deal.ThongTinChinh;
                newDeal.DiemNoiBat = deal.DiemNoiBat;
                newDeal.DieuKien = deal.DieuKien;
                newDeal.ThoiHanSuDungVoucher = deal.ThoiHanSuDungVoucher;
       
                newDeal.NgayDang = deal.NgayDang;
                newDeal.NgayKetThuc = deal.NgayKetThuc;
                newDeal.SoLuongToiDa = deal.SoLuongToiDa;
                newDeal.SoLuongMuaGiaTot = deal.SoLuongMuaGiaTot;
                
                dataContext.DealDaDuyets.InsertOnSubmit(newDeal);

                var listImage = (from image in dataContext.PathImageChuaDuyets
                                 where image.Username == deal.UsernameDoanhNghiep && image.TenDeal == deal.TenDeal
                                 select image
                                ).ToList();
                foreach (PathImageChuaDuyet image in listImage)
                {
                    PathImageDaDuyet temp = new PathImageDaDuyet();
                    temp.Username = image.Username;
                    temp.TenDeal = image.TenDeal;
                    temp.Path = image.Path;
                    temp.DealDaDuyet = newDeal;
                    dataContext.PathImageDaDuyets.InsertOnSubmit(temp);
                }
                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool InsertDealChuaDuyet(RegisterDeal regdeal)
        {
            try
            {
                DealChuaDuyet newDeal = new DealChuaDuyet();
                newDeal.UsernameDoanhNghiep = regdeal.UsernameDoanhNghiep;
                newDeal.TenDeal = regdeal.TenDeal;
                newDeal.TieuDeSanPham = regdeal.TieuDeSanPham;
                newDeal.ThongTinChinh = regdeal.ThongTinChinh;
                newDeal.DieuKien = regdeal.DieuKien;
                newDeal.DiemNoiBat = regdeal.DiemNoiBat;
                newDeal.MaLoaiHinhGiaoDich = regdeal.MaLoaiHinhGiaoDich;
                newDeal.MaLoaiDeal = regdeal.MaLoaiDeal;
                newDeal.GiaGoc = regdeal.GiaGoc;
                newDeal.GiaTien = regdeal.GiaTien;
                newDeal.NgayDang = regdeal.NgayDang;
                newDeal.NgayKetThuc = regdeal.NgayKetThuc;
                newDeal.SoLuongToiDa = regdeal.SoLuongMuaToiDa;
                newDeal.SoLuongMuaGiaTot = regdeal.SoLuongMuaGiaTot;
                dataContext.DealChuaDuyets.InsertOnSubmit(newDeal);


                foreach (string path in regdeal.ListImagesPath)
                {
                    PathImageChuaDuyet pathImage = new PathImageChuaDuyet();
                    pathImage.Username = regdeal.UsernameDoanhNghiep;
                    pathImage.TenDeal = regdeal.TenDeal;
                    pathImage.Path = path;
                    pathImage.DealChuaDuyet = newDeal;
                    dataContext.PathImageChuaDuyets.InsertOnSubmit(pathImage);
                }
                dataContext.SubmitChanges();
                return true;
            }
            catch(Exception ex) {
                
                return false;
            }
        }

        public bool UpdateDeal(Deal deal)
        {
            try
            {
                Deal tempDeal = dataContext.Deals.Single(d => d.MaDeal == deal.MaDeal);
               
                tempDeal.TenDeal = deal.TenDeal;
                tempDeal.UsernameDoanhNghiep = deal.UsernameDoanhNghiep;
                tempDeal.MaLoaiHinhGiaoDich = deal.MaLoaiHinhGiaoDich;
                tempDeal.MaLoaiDeal = deal.MaLoaiDeal;
                tempDeal.GiaGoc = deal.GiaGoc;
                tempDeal.GiaTien = deal.GiaTien;
                tempDeal.NgayDang = deal.NgayDang;
                tempDeal.NgayKetThuc = deal.NgayKetThuc;
                tempDeal.SoLuongToiDa = deal.SoLuongToiDa;
                tempDeal.SoLuongMuaGiaTot = deal.SoLuongMuaGiaTot;
                tempDeal.SoLuongMuaHienTai = deal.SoLuongMuaHienTai;

                dataContext.SubmitChanges();

                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteDeal(int maDeal)
        {
            try
            {
                Deal deal = (from d in dataContext.Deals
                             where d.MaDeal == maDeal
                             select d).First();
                IList<Comment> listcomment = (from comment in dataContext.Comments
                                              where comment.MaDeal == maDeal
                                              select comment).ToList();
                IList<DanhSachSanPham> listsanpham = (from sanpham in dataContext.DanhSachSanPhams
                                              where sanpham.MaDeal == maDeal
                                              select sanpham).ToList();
                dataContext.DanhSachSanPhams.DeleteAllOnSubmit(listsanpham);
                dataContext.Comments.DeleteAllOnSubmit(listcomment);
                dataContext.Deals.DeleteOnSubmit(deal);
                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteDealChuaDuyet(string tenDeal)
        {
            try
            {
                DealChuaDuyet deal = (from d in dataContext.DealChuaDuyets
                             where d.TenDeal == tenDeal
                             select d).First();

                dataContext.DealChuaDuyets.DeleteOnSubmit(deal);
                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public bool DeleteDealDaDuyet(string tenDeal)
        {
            try
            {
                DealDaDuyet deal = (from d in dataContext.DealDaDuyets
                                      where d.TenDeal == tenDeal
                                      select d).First();

                dataContext.DealDaDuyets.DeleteOnSubmit(deal);
                dataContext.SubmitChanges();
                return true;
            }
            catch
            {
                return false;
            }
        }
        public Deal GetDeal(int maDeal)
        {
            return dataContext.Deals.Single(d => d.MaDeal == maDeal);
        }
        public DealChuaDuyet GetDealChuaDuyet(String tenDeal)
        {
            return dataContext.DealChuaDuyets.Single(d => d.TenDeal == tenDeal);
        }
        public DealDaDuyet GetDealDaDuyet(String tenDeal)
        {
            return dataContext.DealDaDuyets.Single(d => d.TenDeal == tenDeal);
        }

        public IList<LoaiHinhGiaoDich> GetAllLoaiHinhGiaoDich()
        {
            var list = dataContext.LoaiHinhGiaoDiches;
            return list.ToList();
        }
        public LoaiHinhGiaoDich GetLoaiHinhGiaoDich(int maLoaiHinh)
        {
            return dataContext.LoaiHinhGiaoDiches.Single(lh => lh.MaLoaiHinhGiaoDich == maLoaiHinh);
        }

        public LoaiDeal GetLoaiDeal(int maLoaiDeal)
        {
            return dataContext.LoaiDeals.Single(ld => ld.MaLoaiDeal == maLoaiDeal);
        }
        public IList<LoaiDeal> GetAllLoaiDeal()
        {
            var list = dataContext.LoaiDeals;
            return list.ToList();
        }

        public IList<Deal> SearchDeal(string tenDeal)
        {

            var listDeal = from o in dataContext.Deals where (o.TenDeal.Contains(tenDeal)) select o;
            return listDeal.ToList();
        }
        public IList<DealChuaDuyet> SearchDealChuaDuyet(string tenDeal)
        {

            var listDeal = from o in dataContext.DealChuaDuyets where (o.TenDeal.Contains(tenDeal)) select o;
            return listDeal.ToList();
        }
        public IList<DealDaDuyet> SearchDealDaDuyet(string tenDeal)
        {

            var listDeal = from o in dataContext.DealDaDuyets where (o.TenDeal.Contains(tenDeal)) select o;
            return listDeal.ToList();
        }


    }
}